/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 *
 * @author Trung
 */
public class TCPClient {
    public static void main(String agrs[])
    {
        try
        {
            Socket sk = new Socket("localhost", 3001);
            //System.out.println("LocalAddress" + sk.getLocalAddress());
            //System.out.println("LocalSocketAddreass" + sk.getLocalSocketAddress());
            //System.out.println("HostAdrress: " + InetAddress.getLocalHost());
            System.out.println(sk.getPort());
            
            InputStream is = sk.getInputStream();
            OutputStream os = sk.getOutputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(os));
            
            String rec = "";
            String send = "";
            do
            {        
                BufferedReader bufR = new BufferedReader(new InputStreamReader(System.in));
                send = bufR.readLine();
                if(send != "")
                {
                    bw.write(send);
                    bw.newLine();
                    bw.flush();
                    //bw.close();
                }
                
                if(send.equalsIgnoreCase("quit"))
                    break;
                                    
                rec = br.readLine();
                if(rec != "")
                    System.out.println("Receive: " + rec);
            }
            while(true);
            br.close();
            bw.close();
        }
        catch(IOException ex)
        {
            System.out.println(ex.getMessage());
        }
    }
}
